'use strict'
require('./index.scss')
require('page/common/nav-simple/index.js')

// 通用的JS工具封装
var _wm = require('util/wm.js')
// 用户服务
var _user = require('service/user-service.js')

// 表单错误信息提示
var formError = {
    show: function(errMsg){
        $('.error_item').show().find('.error_msg').text(errMsg)
    },
    hide: function(){
        $('.error_item').hide().find('.error_msg').text('')
    }
}


// 登录的逻辑
var page = {
    data: {
        username: '',
        question: '',
        answer: '',
        token: ''
    },

    init: function(){
        this.onLoad()
        this.bindEvent()
    },

    // 加载输入用户名的一步
    loadStepUsername: function(){
        $('.step_username').show()
    },
    // 加载输入密码提示问题答案的一步
    loadStepQuestion: function(){
        // 隐藏错误提示
        formError.hide()

        // 切换到密码提示问题的容器
        $('.step_username').hide().siblings('.step_question').show().find('.question').text(this.data.question)
    },
    // 加载输入密码的一步
    loadStepPassword: function(){
        // 隐藏错误提示
        formError.hide()

        // 切换设置新密码的容器
        $('.step_question').hide().siblings('.step_password').show()
    },

    onLoad: function(){
        this.loadStepUsername()
    },

    bindEvent: function(){
        var that = this

        // 输入用户名后下一步的点击
        $('#submit_username').click(function(){
            var username = $.trim($('#username').val())
            if(username){  // 用户名存在
                _user.getQuestion(username, function(res){
                    that.data.username = username
                    that.data.question = res

                    that.loadStepQuestion()
                }, function(errMsg){
                    formError.show(errMsg)
                })
            }else{  // 用户名不存在
                formError.show('请输入用户名')
            }
        })

        // 输入密码提示问题答案中下一步的点击
        $('#submit_question').click(function(){
            var answer = $.trim($('#answer').val())
            if(answer){  // 若答案存在
                // 检测答案
                _user.checkAnswer({
                    username: that.data.username,
                    question: that.data.question,
                    answer: answer
                }, function(res){
                    that.data.answer = answer
                    that.data.token = res

                    that.loadStepPassword()
                }, function(errMsg){
                    formError.show(errMsg)
                })
            }else{  // 若答案不存在
                formError.show('请输入正确的答案')
            }
        })

        // 输入新密码后下一步的点击
        $('#submit_password').click(function(){
            var password = $.trim($('#password').val())
            if(password && password.length >= 6){  // 若密码不为空
                // 重置密码
                _user.resetPassword({
                    username: that.data.username,
                    passwordNew: password,
                    forgetToken: that.data.token
                }, function(res){
                    window.location.href = './result.html?type=pass-reset'
                }, function(errMsg){
                    formError.show(errMsg)
                })
            }else{  
                formError.show('请输入不少于6位的新密码')
            }
        })
    },

    // 登录提交
    submit: function(){
        var formData = {
            username: $.trim($('#username').val()),
            password: $.trim($('#password').val())
        },
        // 返回表单验证的结果true or false
        validateResult = this.formValidate(formData)
        if(validateResult.status){  // 验证成功
            _user.login(formData, function(res){
                window.location.href = _wm.getUrlParam('redirect') || './index.html'
            }, function(errMsg){
                formError.show(errMsg)
            })
        }else{  // 验证失败
            // 错误提示
            formError.show(validateResult.msg)
        }
    },

    // 表单字段的验证
    formValidate: function(formData){
        var result = {
            status: false,
            msg: ''
        }
        if(!_wm.validate(formData.username, 'require')){
            result.msg = '用户名不能为空'
            return result
        }
        if(!_wm.validate(formData.password, 'require')){
            result.msg = '密码不能为空'
            return result
        }
        // 验证通过，返回正确提示
        // result.status = true
        // result.msg = '验证通过'
        // return result
        return result = {
            status: true,
            msg: '验证通过'
        }
    }
}

$(function(){
    page.init()
})